package cn.mumzone.pos.ui.widgets;

import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.JustifyContent;

import java.util.ArrayList;
import java.util.List;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ItemViewDelegate;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.DisplayUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.GoodsTreeEntity;
import cn.mumzone.pos.util.MyFlexBoxLayoutManager;

/**
 * Created by qiuzhenhuan on 2019/7/31.
 */

public class GoodsClassifyView extends LinearLayout {

    private RecyclerView mRvLeftLabel;
    private RecyclerView mRvRightLabel;

    private TopClassifyAdapter mTopClassifyAdapter;
    private ChildClassifyAdapter mChildClassifyAdapter;

    private boolean mDoubleCheck = false;

    public GoodsClassifyView(Context context) {
        this(context, null);
    }

    public GoodsClassifyView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public GoodsClassifyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(LinearLayout.HORIZONTAL);

        mRvLeftLabel = new RecyclerView(context);
        mRvLeftLabel.setBackgroundResource(R.color.bg_color);
        LayoutParams leftParams = new LayoutParams(0, LayoutParams.MATCH_PARENT);
        leftParams.weight = 21;
        mRvLeftLabel.setLayoutParams(leftParams);
        mTopClassifyAdapter = new TopClassifyAdapter(context);
        mRvLeftLabel.setLayoutManager(new LinearLayoutManager(context));
        mRvLeftLabel.setAdapter(mTopClassifyAdapter);
        mRvLeftLabel.setHasFixedSize(true);


        mRvRightLabel = new RecyclerView(context);
        mRvRightLabel.setBackgroundResource(R.color.white_color);
        LayoutParams rightParams = new LayoutParams(0, LayoutParams.MATCH_PARENT);
        rightParams.weight = 79;
        rightParams.topMargin = DisplayUtil.dip2px(context, 16);
        rightParams.leftMargin = DisplayUtil.dip2px(context, 16);
        rightParams.rightMargin = DisplayUtil.dip2px(context, 16);
        mRvRightLabel.setLayoutParams(rightParams);
        MyFlexBoxLayoutManager layoutManager = new MyFlexBoxLayoutManager(context);
        layoutManager.setFlexDirection(FlexDirection.ROW);
        layoutManager.setFlexWrap(FlexWrap.WRAP);
        layoutManager.setJustifyContent(JustifyContent.FLEX_START);
        mRvRightLabel.setLayoutManager(layoutManager);
        mChildClassifyAdapter = new ChildClassifyAdapter(context);
        mRvRightLabel.setAdapter(mChildClassifyAdapter);

        addView(mRvLeftLabel);
        addView(mRvRightLabel);
    }


    public void setDoubleCheck(boolean doubleCheck) {
        this.mDoubleCheck = doubleCheck;
    }

    public void setData(List<GoodsTreeEntity> goodsTreeEntities) {
        List<GoodsTreeEntity> goodsTreeList = new ArrayList<>();
        GoodsTreeEntity goodsTreeEntity = new GoodsTreeEntity();
        goodsTreeEntity.setTitle("全部");
        goodsTreeEntity.setLabel("全部分类");
        goodsTreeEntity.setValue("");
        goodsTreeList.add(goodsTreeEntity);
        goodsTreeList.addAll(goodsTreeEntities);
        mTopClassifyAdapter.update(goodsTreeList, true);

        initChildClassifyTree(goodsTreeList.get(0));
    }

    private void initChildClassifyTree(GoodsTreeEntity topLabel) {

        List<GoodsTreeEntity> childClassifyList = new ArrayList<>();

        GoodsTreeEntity topChildLabel = new GoodsTreeEntity();
        topChildLabel.setTitle("全部");
        topChildLabel.setLabel(topLabel.getLabel());
        topChildLabel.setValue(topLabel.getValue());
        topChildLabel.setViewType(ChildClassifyAdapter.VIEW_TYPE_DEFAULT);
        childClassifyList.add(topChildLabel);

        if (!ListUtil.isListEmpty(topLabel.getChildren())) {
            for (GoodsTreeEntity childTree : topLabel.getChildren()) {

                GoodsTreeEntity titleLabel = new GoodsTreeEntity();
                titleLabel.setViewType(ChildClassifyAdapter.VIEW_TYPE_TITLE);
                titleLabel.setTitle(childTree.getLabel());
                titleLabel.setLabel(childTree.getLabel());
                titleLabel.setValue(childTree.getValue());
                childClassifyList.add(titleLabel);

                GoodsTreeEntity titleLabelChild = new GoodsTreeEntity();
                titleLabelChild.setViewType(ChildClassifyAdapter.VIEW_TYPE_DEFAULT);
                titleLabelChild.setTitle("全部");
                titleLabelChild.setLabel(childTree.getLabel());
                titleLabelChild.setValue(childTree.getValue());
                childClassifyList.add(titleLabelChild);

                if (!ListUtil.isListEmpty(childTree.getChildren())) {
                    for (GoodsTreeEntity lastTree : childTree.getChildren()) {
                        GoodsTreeEntity childLabel = new GoodsTreeEntity();
                        childLabel.setViewType(ChildClassifyAdapter.VIEW_TYPE_DEFAULT);
                        childLabel.setTitle(lastTree.getLabel());
                        childLabel.setLabel(lastTree.getLabel());
                        childLabel.setValue(lastTree.getValue());
                        childClassifyList.add(childLabel);
                    }
                }

            }
        }

        mChildClassifyAdapter.update(childClassifyList, true);
        mRvRightLabel.smoothScrollToPosition(0);
    }

    public void clearSelect(){
        if(!ListUtil.isListEmpty(mChildClassifyAdapter.mSelectedClassify)){
            mChildClassifyAdapter.clearSelect();
        }
    }

    public List<GoodsTreeEntity> getSelectItem(){
        return mChildClassifyAdapter.mSelectedClassify;
    }


    class TopClassifyAdapter extends CommonAdapter<GoodsTreeEntity> {

        private ViewHolder mPreHolder;

        private TopClassifyAdapter(Context context) {
            super(context);
        }

        @Override
        protected int itemLayoutId() {
            return R.layout.item_goods_classify_top;
        }

        @Override
        protected void convert(final ViewHolder holder, GoodsTreeEntity goodsTreeEntity, final int position) {
            TextView tvLabel = holder.getView(R.id.tv_label);
            tvLabel.setText(!StringUtils.isEmpty(goodsTreeEntity.getTitle()) ? goodsTreeEntity.getTitle() : goodsTreeEntity.getLabel());
            if (null == mPreHolder) {
                mPreHolder = holder;
                tvLabel.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                tvLabel.setBackgroundResource(R.color.white_color);
            } else {
                tvLabel.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                tvLabel.setBackgroundResource(R.color.bg_color);
            }

            tvLabel.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mPreHolder == holder) {
                        return;
                    }
                    TextView tvLabel = holder.getView(R.id.tv_label);
                    tvLabel.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    tvLabel.setBackgroundResource(R.color.white_color);

                    if (mPreHolder != null) {
                        TextView mPreTvLabel = mPreHolder.getView(R.id.tv_label);
                        mPreTvLabel.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                        mPreTvLabel.setBackgroundResource(R.color.bg_color);
                    }

                    mPreHolder = holder;

                    if (!mDoubleCheck) {
                        mChildClassifyAdapter.clearSelect();
                    }
                    initChildClassifyTree(mTopClassifyAdapter.getDataList().get(position));

                }
            });
        }

    }


    class ChildClassifyAdapter extends CommonAdapter<GoodsTreeEntity> {

        private static final int VIEW_TYPE_DEFAULT = 0;
        private static final int VIEW_TYPE_TITLE = 1;

        private List<GoodsTreeEntity> mSelectedClassify;

        public ChildClassifyAdapter(Context context) {
            super(context);
            addItemViewDelegate(VIEW_TYPE_TITLE, new ItemViewDelegate<GoodsTreeEntity>() {
                @Override
                public int getItemViewLayoutId() {
                    return R.layout.item_goods_classify_child_title;
                }

                @Override
                public boolean isForViewType(GoodsTreeEntity item, int position) {
                    return true;
                }

                @Override
                public void convert(ViewHolder holder, GoodsTreeEntity goodsTreeEntity, int position) {

                }
            });
            mSelectedClassify = new ArrayList<>();
        }

        @Override
        protected int itemLayoutId() {
            return R.layout.item_goods_classify_child;
        }

        @Override
        protected void convert(final ViewHolder holder, final GoodsTreeEntity goodsTreeEntity, final int position) {

            if (getItemViewType(position) == VIEW_TYPE_TITLE) {
                holder.setText(R.id.tv_classify_title, goodsTreeEntity.getTitle());
            } else {

                holder.setVisible(R.id.iv_checked, checkInSelect(goodsTreeEntity) != null);
                holder.setText(R.id.tv_classify, goodsTreeEntity.getTitle());
                holder.setOnClickListener(R.id.fl_classify, new OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        GoodsTreeEntity localEntity = checkInSelect(goodsTreeEntity);
                        if (localEntity != null) {
                            holder.setVisible(R.id.iv_checked, false);
                            mSelectedClassify.remove(localEntity);
                            if (!mDoubleCheck && onGoodsClassifyClickListener != null) {
                                onGoodsClassifyClickListener.onGoodsClassifyClick(null);
                            }
                        } else {
                            if (!mDoubleCheck) {
                                mSelectedClassify.clear();
                            }
                            holder.setVisible(R.id.iv_checked, true);
                            mSelectedClassify.add(goodsTreeEntity);

                            if (!mDoubleCheck) {
                                notifyDataSetChanged();
                            }

                            if (!mDoubleCheck && onGoodsClassifyClickListener != null) {
                                onGoodsClassifyClickListener.onGoodsClassifyClick(goodsTreeEntity);
                            }
                        }

                    }
                });
            }
        }

        private GoodsTreeEntity checkInSelect(GoodsTreeEntity currentEntity) {
            for (GoodsTreeEntity localEntity : mSelectedClassify) {
                if (localEntity.getValue().equals(currentEntity.getValue())) {
                    return localEntity;
                }
            }
            return null;
        }


        @Override
        public int getItemViewType(int position) {
            GoodsTreeEntity entity = getDataList().get(position);
            return entity.getViewType();
        }

        public void clearSelect() {
            mSelectedClassify.clear();
            notifyDataSetChanged();
        }
    }

    private OnGoodsClassifyClickListener onGoodsClassifyClickListener;

    public void setOnGoodsClassifyClickListener(OnGoodsClassifyClickListener onGoodsClassifyClickListener) {
        this.onGoodsClassifyClickListener = onGoodsClassifyClickListener;
    }

    public interface OnGoodsClassifyClickListener {

        void onGoodsClassifyClick(GoodsTreeEntity goodsTreeEntity);
    }

}
